﻿#include "common.h"

int getPositiveIntInput(PostiveInputConfig config) {
	cout << config.message;

	while (true) {

		string line;

		getline(cin, line);

		if (line.size() <= 0) {
			cout << config.message;
			continue;
		}

		try {
			int num = stoi(line);

			if (num < 1 || num > config.max) {
				cout << "输入错误，" << config.message;
				continue;
			}

			return num;
		}
		catch (invalid_argument error) {
			cout << "输入错误，" << config.message;
		}
	}
}

string getLineInput(LineInputConfig config) {
	cout << config.message;

	outer: while (true) {

		string line;

		getline(cin, line);

		if (line.size() <= 0) {
			cout << config.message;
			continue;
		}

		if (line.size() > config.max_length) {
			cout << "长度不能超过" << config.max_length << "，" << config.message;
			continue;
		}

		for (char ban_char : config.ban_chars) {
			auto result = line.find(ban_char);
			if (result != -1) {
				cout << "不能使用" << ban_char << "，请重新输入：";
				goto outer;
			}
		}

		return line;
	}
}

int getMenuSelectInput(MenuSelectConfig config) {
	cout << "请选择" << endl;

	for (auto menu : config.menus) {
		cout << menu.text << "(" << menu.value << ")" << endl;
	}

	cout << endl;

	while (true) {
		int select = getPositiveIntInput({ "请输入：", LONG_MAX });

		bool inMenus = false;

		for (auto menu : config.menus) {
			if (menu.value == select) {
				inMenus = true;
				break;
			}
		}

		if (!inMenus) {
			cout << "输入错误";
			continue;
		}

		return select;
	}
}